<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of gtminit</title>
  <meta name="keywords" content="gtminit">
  <meta name="description" content="GTMINIT Initialise the weights and latent sample in a GTM.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; gtminit.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>gtminit
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>GTMINIT Initialise the weights and latent sample in a GTM.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function net = gtminit(net, options, data, samp_type, varargin) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">GTMINIT Initialise the weights and latent sample in a GTM.

    Description
    NET = GTMINIT(NET, OPTIONS, DATA, SAMPTYPE) takes a GTM NET and
    generates a sample of latent data points and sets the centres (and
    widths if appropriate) of NET.RBFNET.

    If the SAMPTYPE is 'REGULAR', then regular grids of latent data
    points and RBF centres are created.  The dimension of the latent data
    space must be 1 or 2.  For one-dimensional latent space, the
    LSAMPSIZE parameter gives the number of latent points and the
    RBFSAMPSIZE parameter gives the number of RBF centres.  For a two-
    dimensional latent space, these parameters must be vectors of length
    2 with the number of points in each of the x and y directions to
    create a rectangular grid.  The widths of the RBF basis functions are
    set by a call to RBFSETFW passing OPTIONS(7) as the scaling
    parameter.

    If the SAMPTYPE is 'UNIFORM' or 'GAUSSIAN' then the latent data is
    found by sampling from a uniform or Gaussian distribution
    correspondingly.  The RBF basis function parameters are set by a call
    to RBFSETBF with the DATA parameter as dataset and the OPTIONS
    vector.

    Finally, the output layer weights of the RBF are initialised by
    mapping the mean of the latent variable to the mean of the target
    variable, and the L-dimensional latent variale variance to the
    variance of the targets along the first L principal components.

    See also
    <a href="gtm.html" class="code" title="function net = gtm(dim_latent, nlatent, dim_data, ncentres, rbfunc,prior)">GTM</a>, <a href="gtmem.html" class="code" title="function [net, options, errlog] = gtmem(net, t, options)">GTMEM</a>, <a href="pca.html" class="code" title="function [PCcoeff, PCvec] = pca(data, N)">PCA</a>, <a href="rbfsetbf.html" class="code" title="function net = rbfsetbf(net, options, x)">RBFSETBF</a>, <a href="rbfsetfw.html" class="code" title="function net = rbfsetfw(net, scale)">RBFSETFW</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="consist.html" class="code" title="function errstring = consist(model, type, inputs, outputs)">consist</a>	CONSIST Check that arguments are consistent.</li><li><a href="dist2.html" class="code" title="function n2 = dist2(x, c)">dist2</a>	DIST2	Calculates squared distance between two sets of points.</li><li><a href="pca.html" class="code" title="function [PCcoeff, PCvec] = pca(data, N)">pca</a>	PCA	Principal Components Analysis</li><li><a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>	RBFFWD	Forward propagation through RBF network with linear outputs.</li><li><a href="rbfsetbf.html" class="code" title="function net = rbfsetbf(net, options, x)">rbfsetbf</a>	RBFSETBF Set basis functions of RBF from data.</li><li><a href="rbfsetfw.html" class="code" title="function net = rbfsetfw(net, scale)">rbfsetfw</a>	RBFSETFW Set basis function widths of RBF.</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="demgtm1.html" class="code" title="">demgtm1</a>	DEMGTM1 Demonstrate EM for GTM.</li><li><a href="demgtm2.html" class="code" title="">demgtm2</a>	DEMGTM2 Demonstrate GTM for visualisation.</li></ul>
<!-- crossreference -->

<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="#_sub1" class="code">function sample = gtm_rctg(samp_size)</a></li></ul>
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function net = gtminit(net, options, data, samp_type, varargin)</a>
0002 <span class="comment">%GTMINIT Initialise the weights and latent sample in a GTM.</span>
0003 <span class="comment">%</span>
0004 <span class="comment">%    Description</span>
0005 <span class="comment">%    NET = GTMINIT(NET, OPTIONS, DATA, SAMPTYPE) takes a GTM NET and</span>
0006 <span class="comment">%    generates a sample of latent data points and sets the centres (and</span>
0007 <span class="comment">%    widths if appropriate) of NET.RBFNET.</span>
0008 <span class="comment">%</span>
0009 <span class="comment">%    If the SAMPTYPE is 'REGULAR', then regular grids of latent data</span>
0010 <span class="comment">%    points and RBF centres are created.  The dimension of the latent data</span>
0011 <span class="comment">%    space must be 1 or 2.  For one-dimensional latent space, the</span>
0012 <span class="comment">%    LSAMPSIZE parameter gives the number of latent points and the</span>
0013 <span class="comment">%    RBFSAMPSIZE parameter gives the number of RBF centres.  For a two-</span>
0014 <span class="comment">%    dimensional latent space, these parameters must be vectors of length</span>
0015 <span class="comment">%    2 with the number of points in each of the x and y directions to</span>
0016 <span class="comment">%    create a rectangular grid.  The widths of the RBF basis functions are</span>
0017 <span class="comment">%    set by a call to RBFSETFW passing OPTIONS(7) as the scaling</span>
0018 <span class="comment">%    parameter.</span>
0019 <span class="comment">%</span>
0020 <span class="comment">%    If the SAMPTYPE is 'UNIFORM' or 'GAUSSIAN' then the latent data is</span>
0021 <span class="comment">%    found by sampling from a uniform or Gaussian distribution</span>
0022 <span class="comment">%    correspondingly.  The RBF basis function parameters are set by a call</span>
0023 <span class="comment">%    to RBFSETBF with the DATA parameter as dataset and the OPTIONS</span>
0024 <span class="comment">%    vector.</span>
0025 <span class="comment">%</span>
0026 <span class="comment">%    Finally, the output layer weights of the RBF are initialised by</span>
0027 <span class="comment">%    mapping the mean of the latent variable to the mean of the target</span>
0028 <span class="comment">%    variable, and the L-dimensional latent variale variance to the</span>
0029 <span class="comment">%    variance of the targets along the first L principal components.</span>
0030 <span class="comment">%</span>
0031 <span class="comment">%    See also</span>
0032 <span class="comment">%    GTM, GTMEM, PCA, RBFSETBF, RBFSETFW</span>
0033 <span class="comment">%</span>
0034 
0035 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0036 
0037 <span class="comment">% Check for consistency</span>
0038 errstring = <a href="consist.html" class="code" title="function errstring = consist(model, type, inputs, outputs)">consist</a>(net, <span class="string">'gtm'</span>, data);
0039 <span class="keyword">if</span> ~isempty(errstring)
0040   error(errstring);
0041 <span class="keyword">end</span>
0042 
0043 <span class="comment">% Check type of sample</span>
0044 stypes = {<span class="string">'regular'</span>, <span class="string">'uniform'</span>, <span class="string">'gaussian'</span>};
0045 <span class="keyword">if</span> (strcmp(samp_type, stypes)) == 0
0046   error(<span class="string">'Undefined sample type.'</span>)
0047 <span class="keyword">end</span>
0048 
0049 <span class="keyword">if</span> net.dim_latent &gt; size(data, 2)
0050   error(<span class="string">'Latent space dimension must not be greater than data dimension'</span>)
0051 <span class="keyword">end</span>
0052 nlatent = net.gmmnet.ncentres;
0053 nhidden = net.rbfnet.nhidden;
0054 
0055 <span class="comment">% Create latent data sample and set RBF centres</span>
0056 
0057 <span class="keyword">switch</span> samp_type
0058 <span class="keyword">case</span> <span class="string">'regular'</span>
0059    <span class="keyword">if</span> nargin ~= 6
0060       error(<span class="string">'Regular type must specify latent and RBF shapes'</span>);
0061    <span class="keyword">end</span>
0062    l_samp_size = varargin{1};
0063    rbf_samp_size = varargin{2};
0064    <span class="keyword">if</span> round(l_samp_size) ~= l_samp_size
0065       error(<span class="string">'Latent sample specification must contain integers'</span>)
0066    <span class="keyword">end</span>
0067    <span class="comment">% Check existence and size of rbf specification</span>
0068    <span class="keyword">if</span> any(size(rbf_samp_size) ~= [1 net.dim_latent]) | <span class="keyword">...</span>
0069          prod(rbf_samp_size) ~= nhidden
0070       error(<span class="string">'Incorrect specification of RBF centres'</span>)
0071    <span class="keyword">end</span>
0072    <span class="comment">% Check dimension and type of latent data specification</span>
0073    <span class="keyword">if</span> any(size(l_samp_size) ~= [1 net.dim_latent]) | <span class="keyword">...</span>
0074          prod(l_samp_size) ~= nlatent
0075       error(<span class="string">'Incorrect dimension of latent sample spec.'</span>)
0076    <span class="keyword">end</span>
0077    <span class="keyword">if</span> net.dim_latent == 1
0078       net.X = [-1:2/(l_samp_size-1):1]';
0079       net.rbfnet.c = [-1:2/(rbf_samp_size-1):1]';
0080       net.rbfnet = <a href="rbfsetfw.html" class="code" title="function net = rbfsetfw(net, scale)">rbfsetfw</a>(net.rbfnet, options(7));
0081    <span class="keyword">elseif</span> net.dim_latent == 2
0082       net.X = <a href="#_sub1" class="code" title="subfunction sample = gtm_rctg(samp_size)">gtm_rctg</a>(l_samp_size);
0083       net.rbfnet.c = <a href="#_sub1" class="code" title="subfunction sample = gtm_rctg(samp_size)">gtm_rctg</a>(rbf_samp_size);
0084       net.rbfnet = <a href="rbfsetfw.html" class="code" title="function net = rbfsetfw(net, scale)">rbfsetfw</a>(net.rbfnet, options(7));
0085    <span class="keyword">else</span>
0086       error(<span class="string">'For regular sample, input dimension must be 1 or 2.'</span>)
0087    <span class="keyword">end</span>
0088    
0089    
0090 <span class="keyword">case</span> {<span class="string">'uniform'</span>, <span class="string">'gaussian'</span>}
0091    <span class="keyword">if</span> strcmp(samp_type, <span class="string">'uniform'</span>)
0092       net.X = 2 * (rand(nlatent, net.dim_latent) - 0.5);
0093    <span class="keyword">else</span>
0094       <span class="comment">% Sample from N(0, 0.25) distribution to ensure most latent</span>
0095       <span class="comment">% data is inside square</span>
0096       net.X = randn(nlatent, net.dim_latent)/2;
0097    <span class="keyword">end</span>   
0098    net.rbfnet = <a href="rbfsetbf.html" class="code" title="function net = rbfsetbf(net, options, x)">rbfsetbf</a>(net.rbfnet, options, net.X);
0099 <span class="keyword">otherwise</span>
0100    <span class="comment">% Shouldn't get here</span>
0101    error(<span class="string">'Invalid sample type'</span>);
0102    
0103 <span class="keyword">end</span>
0104 
0105 <span class="comment">% Latent data sample and basis function parameters chosen.</span>
0106 <span class="comment">% Now set output weights</span>
0107 [PCcoeff, PCvec] = <a href="pca.html" class="code" title="function [PCcoeff, PCvec] = pca(data, N)">pca</a>(data);
0108 
0109 <span class="comment">% Scale PCs by eigenvalues</span>
0110 A = PCvec(:, 1:net.dim_latent)*diag(sqrt(PCcoeff(1:net.dim_latent)));
0111 
0112 [temp, Phi] = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net.rbfnet, net.X);
0113 <span class="comment">% Normalise X to ensure 1:1 mapping of variances and calculate weights</span>
0114 <span class="comment">% as solution of Phi*W = normX*A'</span>
0115 normX = (net.X - ones(size(net.X))*diag(mean(net.X)))*diag(1./std(net.X));
0116 net.rbfnet.w2 = Phi \ (normX*A');
0117 <span class="comment">% Bias is mean of target data</span>
0118 net.rbfnet.b2 = mean(data);
0119 
0120 <span class="comment">% Must also set initial value of variance</span>
0121 <span class="comment">% Find average distance between nearest centres</span>
0122 <span class="comment">% Ensure that distance of centre to itself is excluded by setting diagonal</span>
0123 <span class="comment">% entries to realmax</span>
0124 net.gmmnet.centres = <a href="rbffwd.html" class="code" title="function [a, z, n2] = rbffwd(net, x)">rbffwd</a>(net.rbfnet, net.X);
0125 d = <a href="dist2.html" class="code" title="function n2 = dist2(x, c)">dist2</a>(net.gmmnet.centres, net.gmmnet.centres) + <span class="keyword">...</span>
0126   diag(ones(net.gmmnet.ncentres, 1)*realmax);
0127 sigma = mean(min(d))/2;
0128 
0129 <span class="comment">% Now set covariance to minimum of this and next largest eigenvalue</span>
0130 <span class="keyword">if</span> net.dim_latent &lt; size(data, 2)
0131   sigma = min(sigma, PCcoeff(net.dim_latent+1));
0132 <span class="keyword">end</span>
0133 net.gmmnet.covars = sigma*ones(1, net.gmmnet.ncentres);
0134 
0135 <span class="comment">% Sub-function to create the sample data in 2d</span>
0136 <a name="_sub1" href="#_subfunctions" class="code">function sample = gtm_rctg(samp_size)</a>
0137 
0138 xDim = samp_size(1);
0139 yDim = samp_size(2);
0140 <span class="comment">% Produce a grid with the right number of rows and columns</span>
0141 [X, Y] = meshgrid([0:1:(xDim-1)], [(yDim-1):-1:0]);
0142 
0143 <span class="comment">% Change grid representation</span>
0144 sample = [X(:), Y(:)];
0145 
0146 <span class="comment">% Shift grid to correct position and scale it</span>
0147 maxXY= max(sample);
0148 sample(:,1) = 2*(sample(:,1) - maxXY(1)/2)./maxXY(1);
0149 sample(:,2) = 2*(sample(:,2) - maxXY(2)/2)./maxXY(2);
0150 <span class="keyword">return</span>;
0151 
0152    
0153</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>